<?php
namespace app\admin\controller;

class AuthRole extends Base
{

    /**
     * 角色列表
     * @return html
     */
    public function index()
    {
        $where   = [];
        $title = trim( input( 'title' ) );
        $title && $where['title'] = ['like','%'.$title.'%'];

        $order = 'id DESC';
        $field = 'id, title, module, status, info';

        //分页查询列表
        $list  = db( 'auth_role' )->where( $where )->field( $field )->order( $order )
                 ->paginate( 20, false, ['query'=>request()->param()] );
        //获取分页数据
        $data  = $list->items();

        $this->assign( 'data', $data ); // 使用数据
        $this->assign( 'list', $list ); // 使用分页
        return $this->fetch();
    }
    
    /**
     * 添加角色
     * @return html
     */
    public function add()
    {
        if( request()->isPost() ) {
            $data  = input();
            $data  = array_map( 'trim', $data );
            
                $insertId = db( 'auth_role' )->insertGetId( $data );
                if( $insertId > 0 ) {
                    //操作日志
                    add_system_log( $this->uid, 1, 1, '添加角色' );
                    $this->success( '添加成功', url( 'AuthRole/index' ) );
                } else {
                    $this->error( '添加失败' );
                }
            
        } else {
            return $this->fetch();
        }
    }
    
    /**
     * 编辑角色
     * @return html
     */
    public function edit()
    {
        $id = intval( input( 'id' ) );
        if( !$id ) {
            $this->error( '非法操作' );
        }
        if( request()->isPost() ) {
            $data  = input();
            $data  = array_map( 'trim', $data );
			$valid 		 = new \app\admin\validate\AuthRole;
            if( !$valid->check( $data ) ) {
                $this->error( $valid->getError() );
            } else {
                $updateData          = [];
                $updateData['title'] = $data['title'];
                $updateData['info']  = $data['info'] ? $data['info'] : '';
                $bool = db( 'auth_role' )->where( ['id'=>$id] )->update( $updateData );
                if( $bool !== false ) {
                    //操作日志
                    add_system_log( $this->uid, 1, 2, '编辑角色' );
                    $this->success( '编辑成功', url( 'AuthRole/index' ) );
                } else {
                    $this->error( '编辑失败' );
                }
            }
        } else {
            $field = 'id, title, info';
            $find  = db( 'auth_role' )->field( $field )->where( ['id'=>$id] )->find();
            $this->assign( 'find', $find );
            return $this->fetch();
        }
    }
    
    /**
     * 状态
     * @return html
     */
    public function state()
    {
        $id     = input( 'id' );
        $status = input( 'status' );
        if( $id && $status ) {
            $map = [
                'id' => ['in',$id],
            ];
            $bool = db( 'auth_role' )->where( $map )->update( ['status'=>$status] );
            if( $bool !== false ) {
                //操作日志
                $acname = $status == 1 ? '启用' : '禁用';
                add_system_log( $this->uid, 1, 2, $acname.'角色' );
                $this->success( '设置成功' );
            } else {
                $this->error( '设置失败' );
            }
        } else {
            $this->error( '参数有误' );
        }
    }
    
    /**
     * 删除
     * @return html
     */
    public function remove()
    {
        $id    = input( 'id' );
        $id    = $id ? $id : '';
		
			$idArr =$id;
		

        if( $idArr ) {
            $where            = [];
            $where['role_id'] = ['in', $id];
            $exists = db( 'auth_role_member' )->where( $where )->count();
            if( $exists ) {
                $this->error( '不能删除,该角色下还有管理员' );exit;
            }

            $where       = [];
            $where['id'] = ['in', $idArr];
            if( db( 'auth_role' )->where( $where )->delete() ) {
                $this->success( '删除成功' );
            } else {
                $this->error( '删除失败' );
            }
        } else {
            $this->error( '提交的数据有误' );
        }
    }

    /**
     * 数据权限
     * @return html
     */
    public function dataAuth()
    {
        $id = intval( input( 'id' ) );
        if( !$id ) {
            $this->error( '非法操作' );
        }
        if( request()->isPost() ) {
            $dataAuth = intval( input( 'data_auth' ) );
            if( !$dataAuth ) {
                $this->error( '请选择数据权限' );
            }

            $bool = db( 'auth_role' )->where( ['id'=>$id] )->update( ['data_auth'=>$dataAuth] );
            if( $bool !== false ) {
                //查看该角色已分配的所有用户
                $uidArr = db( 'auth_role_member' )->where( ['role_id'=>$id] )->column( 'uid' );
                if( $uidArr ) {
                    //获取第一个数据权限岗位
                    $where              = [];
                    $where['data_auth'] = $dataAuth;
                    $order  = 'id ASC';
                    $postId = db( 'auth_post' )->where( $where )->order( $order )
                              ->limit(1)->value( 'id' );
                    if( $postId ) {
                        //把改角色下的所有用户的岗位数据权限都更新为新的岗位数据权限
                        //默认第一个
                        $where        = [];
                        $where['uid'] = ['in', $uidArr];
                        db( 'auth_member' )->where( $where )->update( ['post_id'=>$postId] );
                    }
                }

                //操作日志
                add_system_log( $this->uid, 1, 2, '设置数据权限' );
                $this->success( '设置成功', url('AuthRole/index') );
            } else {
                $this->error( '设置失败' );
            }
        } else {
            $dataAuthArr = config( 'role_data_auth' );//数据权限
            $where       = [];
            $where['id'] = $id;
            $field = 'id, title, data_auth';
            $find  = db( 'auth_role' )->field( $field )->where( $where )->find();
            $this->assign( 'find', $find );
            $this->assign( 'dataAuthArr', $dataAuthArr );
            return $this->fetch( 'data_auth' );
        }
    }
    
}